﻿using SD.DataAccess;
using SD.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;

namespace SD.Business
{
    public class ResourceService : IResourceService
    {
        private ResourceRepository dRepo;
        public ResourceService()
        {
            dRepo = new ResourceRepository();
        }

        //最后还是到第三个重载
        public List<IResource> GetResourceByCode(string code)
        {
            return GetResourceByCode(code, false);
        }

        //最后还是到第三个重载
        public List<IResource> GetResourceByCode(string code, bool hasAllOption)
        {
            return GetResourceByCode(code, "", hasAllOption);
        }

        public List<IResource> GetResourceByCode(string code, string parentCode, bool hasAllOption)
        {
            //这里使用了缓存，暂时不用
            //object cache = CacheFactory.Instance.CreateCoreCacheInstance().Get("RESOURCE_" + code + "_" + parentCode); 
            //List<IResource> list = null;
            //if (cache != null)
            //{
            //    list = cache as List<IResource>;
            //}
            //if (list != null)
            //{
            //    if (hasAllOption)
            //        list.Insert(0, new DefaultResource() { Code = null, Name = "请选择...", Value = null });
            //    return list;
            //}

            List<IResource> list = null;
            switch (code)
            {
                case "DelFlag": // 删除标识
                    list = new List<IResource>();
                    list.Add(new DefaultResource() { Code = "0", Name = "正常", Value = "0" });
                    list.Add(new DefaultResource() { Code = "1", Name = "已删除", Value = "1" });
                    break;
                case "ActionType": // 权限枚举
                    list = new List<IResource>();
                    list.Add(new DefaultResource() { Code = "0", Name = "功能权限", Value = "0" });
                    list.Add(new DefaultResource() { Code = "1", Name = "菜单权限", Value = "1" });
                    break;
                case "HttpMethod": // 访问方式
                    list = new List<IResource>();
                    list.Add(new DefaultResource() { Code = "GET", Name = "GET", Value = "GET" });
                    list.Add(new DefaultResource() { Code = "POST", Name = "POST", Value = "POST" });
                    break;
                
                case "DelFlag222"://删除标识
                    list = new List<IResource>();
                    var ulist = dRepo.QueryUserInfo();
                    list = ulist.ConvertAll<IResource>(x =>
                    {
                        return new DefaultResource() { Code = x.Id.ToString(), Name = x.UName, Value = x.Id.ToString() };
                    });

                    break;
               
                

            }
            //这里也是添加缓存暂时不需要
            //if (list != null)
            //{
            //    CacheFactory.Instance.CreateCoreCacheInstance().Insert("RESOURCE_" + code + "_" + parentCode, list);
            //}

            if (hasAllOption)
                list.Insert(0, new DefaultResource() { Code = null, Name = "-----请选择-----", Value = null });
            return list;
        }
    }

    [DataContract]
    public class DefaultResource : IResource
    {
        [DataMember(Name = "code")]
        public string Code
        {
            get;
            set;
        }
        [DataMember(Name = "name")]
        public string Name
        {
            get;
            set;
        }
        [DataMember(Name = "value")]
        public string Value
        {
            get;
            set;
        }
    }
}
